<template lang="html">
  <div class="personal">
    <div class="banner12">
      <label id="fanhui" @click="back"><img src="../../static/img/return1.png" alt=""></label>
      <label>编辑资料</label>
      <span @click="savemessage()">保存</span>
    </div>
    <div class="pecontent">
      <div class="pesetting1">
        <div class="pesetting_name">
          <label>基本资料</label>
        </div>
        <div class="penight">
          <label>头像</label>
          <!-- <input type="checkbox" name="" value=""> -->
            <img v-bind:src="imgsrc" id="ttou" @click="setAvatar">
            <input type="file" name="avatar" accept="image/gif,image/jpeg,image/jpg,image/png" style="display:none" @change="changeImage($event)" ref="avatarInput">
        </div>
        <div class="liuliang">
          <label>背景</label>
          <img v-bind:src="imgsrc1" alt="" @click="setAvatar1">
          <input type="file" name="avatar" accept="image/gif,image/jpeg,image/jpg,image/png" style="display:none" @change="changeImage1($event)" ref="avatarInput1">
        </div>
        <div class="huancun">
          <label>昵称</label>
          <span @click="shname">{{ nicheng }}</span>
        </div>
      </div>
      <div class="pefankui">
        <div class="pefankuiname">
          <label>隐私</label>
        </div>
        <div class="penight">
          <label>隐藏小记</label>
          <!-- <input type="checkbox" name="" value=""> -->
          <input type="checkbox" name="checkbox1" value="1" v-model="xj">
        </div>
        <div class="penight">
          <label>隐藏歌单</label>
          <!-- <input type="checkbox" name="" value=""> -->
          <input type="checkbox" name="checkbox2" value="1" v-model="gd">
        </div>
        <div class="penight">
          <label>隐藏其他收藏</label>
          <!-- <input type="checkbox" name="" value=""> -->
          <input type="checkbox" name="checkbox3" value="1" v-model="qt">
        </div>
      </div>
      <p id="pppp">为保护用户隐私，小记默认设置为仅自己可见。</p>
    </div>
    <div id="gainicheng" v-show='showname'>
      <input type="text" name="" value="" placeholder="昵称" v-model="rename">
      <label @click="baocun()">保存</label>
      <label @click="quxiao()">取消</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nicheng: '',
      xj: false,
      gd: false,
      qt: false,
      imgsrc: '../../static/img/touxiang.jpg',
      imgsrc1: '../../static/img/right.png',
      showname: false,
      rename: '',
      backimg: '../../static/img/loginback.jpg',
      uid:''
    }
  },
  methods: {
    back() {
      history.back()
    },
    setAvatar() {
      this.$refs.avatarInput.click()
    },
    changeImage(e) {
      console.log(this.imgsrc);
      var file = e.target.files[0];
      var reader = new FileReader();
      var that = this;
      if (file.size < 20000) {
        reader.readAsDataURL(file);
        reader.onloadend = function() {
          that.imgsrc = this.result;
        }
      } else {
        alert('选择的图片过大')
      }
      console.log(file.size);
      console.log(this);
    },
    setAvatar1() {
      this.$refs.avatarInput1.click()
    },
    changeImage1(e) {
      console.log(this.imgsrc);
      var file = e.target.files[0];
      var reader = new FileReader();
      var that = this;
      if (file.size < 64000) {
        reader.readAsDataURL(file);
        reader.onloadend = function() {
          that.backimg = this.result;
        }
      } else {
        alert('选择的图片过大')
      }
      console.log(file.size);
      console.log(this);
    },
    shname() {
      this.showname = true
    },
    baocun() {
      var ren=this.rename.replace(/\s+/g, "");
      if(ren!=''){
        this.nicheng = ren;
        this.showname = false
      }
    },
    quxiao() {
      this.showname = false
    },
    savemessage() {
      var datape={
        userid:this.uid,
        username:this.nicheng,
        userphoto:this.imgsrc,
        userbackimg:this.backimg
      }
      // console.log(datape);
      this.axios.post('http://10.0.156.247:8888/updateper', datape, {
          headers: {
            'Content-Type': 'application/json'
          }
        })
        .then(function(response) {
          // console.log(response.config.data);
          var redata=JSON.parse(response.config.data);
          // console.log(redata);
          localStorage.setItem('username',redata.username);
          localStorage.setItem('perphoto',redata.userphoto);
          localStorage.setItem('perbackimg',redata.userbackimg);
        })
        .catch(function(error) {
          console.log(error);
        });
    }
    // edit() {
    //   // 修改了头像
    //   if (this.$refs.avatarInput.files.length !== 0) {
    //     var image = new FormData()
    //     image.append('avatar', this.$refs.avatarInput.files[0]);
    //     this.axios.post('http://10.0.156.247:8888/tupian', image, {
    //       headers: {
    //         "Content-Type": "multipart/form-data"
    //       }
    //     })
    //   }
    // }
  },
  watch: {
    xj(newValue, oldValue) {
      console.log(this.xj);
      if (this.xj == true) {
        localStorage.setItem('xj', 1);
      } else {
        localStorage.setItem('xj', 0);
      }
      console.log(localStorage.getItem('xj'));;
    },
    gd(newValue, oldValue) {
      console.log(this.gd);
      if (this.gd == true) {
        localStorage.setItem('gd', 1);
      } else {
        localStorage.setItem('gd', 0);
      }
      console.log(localStorage.getItem('gd'));;
    },
    qt(newValue, oldValue) {
      console.log(this.qt);
      if (this.qt == true) {
        localStorage.setItem('qt', 1);
      } else {
        localStorage.setItem('qt', 0);
      }
      console.log(localStorage.getItem('qt'));;
    }
  },
  created() {
    this.nicheng = localStorage.getItem('username');
    this.uid=localStorage.getItem('userids');
    this.imgsrc=localStorage.getItem('perphoto');
    // console.log(localStorage.getItem('userids'));
    console.log(localStorage.getItem('xj'), localStorage.getItem('gd'), localStorage.getItem('qt'));
    if (localStorage.getItem('xj') == 0) {
      console.log('hahha');
      this.xj = false;
    } else {
      this.xj = true;
    }
    // else{
    //   console.log('ddd');
    //   this.yxj=1;
    // }
    if (localStorage.getItem('gd') == 0) {
      this.gd = false;
    } else {
      this.gd = true;
    }
    // else{
    //   this.gd=1;
    // }
    if (localStorage.getItem('qt') == 0) {
      this.qt = false;
    } else {
      this.qt = true;
    }
    // else{
    //   this.qt=1;
    // }
  }
}
</script>
<style media="screen">
body {
  margin: 0;
  padding: 0;
}
</style>
<style lang="css" scoped>
*{
  margin:0;
  padding:0;
  color: black;
}

.personal{
  height: 700px;
  position: relative;
  /*background: linear-gradient(to bottom,#562e37,#f17c67);*/
  background:white;
  /*color: white;*/
  }

  .banner12{
    height: 2.5rem;
    /*background: white;*/
    line-height: 2.5rem;
    color: #646a58;
    font-size: 1.8rem;
    width: 100%;
    position:fixed;
    top: 0;
    left: 0;
    /*z-index: 100;*/
    background: white;
    margin-top: 0;
  }
  .banner12>span{
    float: right;
    margin-right: 1.8rem
  }
  #fanhui{
    position: absolute;
    height: 1.5rem;
    width: 0.9rem;
    /*background-image: url(../../static/img/return1.png);
    background-size: 15px,15px;*/
    top: 0.3rem;
    left: 0.9rem;
  }
  #fanhui>img{
    height: 1.5rem;
    width: 0.8rem;
  }
  .pecontent{
    margin: 0 auto;
    margin-top: 3.0rem;
    width: 100%;
    text-align: right;
  }
  .personal>.pecontent>div>div:first-of-type{
    height: 1.8rem;
    line-height: 1.8rem;
    background: #fbfbfb;
    padding-left: 1.5rem;
    padding-right: 1.5rem;
    margin-left: 0;
  }
  .pecontent>div>div:nth-child(n+1){
    height: 3.0rem;
    line-height: 3.0rem;
    padding-left: 1.5rem;
    padding-right: 1.5rem;
    border-bottom: 1px solid #f9f5f5;
    margin-left: 0.5rem;
  }
  .pecontent>div>div>label{
    float: left;
  }
  .pecontent>div>div>img{
    width: 0.5rem;
    height: 1.2rem;
  }
 #pppp{
   background: #fbfbfb;
   height: 1.8rem;
   line-height: 1.8rem;
   font-size: 1.2rem;
   color: gray;
   text-align: left;
   padding-left: 0.5rem;
 }
 #ttou{
   height: 2.5rem;
   width: 2.5rem;
   border-radius: 50%;
   vertical-align: middle;
 }
#gainicheng{
  position: fixed;
  top: 0;
  left: 0;
  height: 700px;
  width: 100%;
  background: rgba(0, 0, 0, 0.4);
}
#gainicheng>input{
  position: absolute;
  top: 30%;
  left: 15%;
  width: 70%;
  height: 2.5rem;
  border: none;
  border-radius: 10px;
  /*z-index: 300;*/
}
#gainicheng>label:first-of-type{
  position: absolute;
  top: 37%;
  left: 15%;
  display: inline-block;
  height: 2.5rem;
  line-height: 2.5rem;
  width: 70%;
  color: black;
  background: white;
  border-radius: 10px;
  font-size: 1.8rem;
  background:#69bae2;
}
#gainicheng>label:last-of-type{
  position: absolute;
  top: 44%;
  left: 15%;
  display: inline-block;
  height: 2.5rem;
  line-height: 2.5rem;
  width: 70%;
  color: black;
  background: white;
  border-radius: 10px;
  font-size: 1.8rem;
  background:#69bae2;
}
</style>
